﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Transformer.iHunter
{
    class ChannelWrapper : IChannel
    {
        IChannel channel;

        public ChannelWrapper(IChannel channel)
        {
            this.channel = channel;
            channel.ErrorOccur += new PluginErrorEventHandler(channel_ErrorOccur);
        }

        void channel_ErrorOccur(object sender, PluginErrorEventArgs e)
        {
            if (this.ErrorOccur != null)
                ErrorOccur(this, e);
        }

        public IChannel Channel
        {
            get
            {
                return channel;
            }
        }

        protected void OnPluginError(Exception e)
        {
            if (ErrorOccur != null)
                ErrorOccur.Invoke(this, new PluginErrorEventArgs(channel, e.Message));
        }



        #region IChannel Members

        public IEnumerable<IMessage> DownloadMessages(DateTime startTime, DateTime endTime, string attachmentDirectoryPath)
        {
            try
            {
                return channel.DownloadMessages(startTime, endTime, attachmentDirectoryPath);
            }
            catch (System.Net.Sockets.SocketException e)
            {
                throw e;
            }
            catch (System.Net.WebException e)
            {
                throw e;
            }
            catch (System.InvalidOperationException e)
            {
                throw e;
            }
            catch (Exception e)
            {
                this.OnPluginError(e);
                throw new InvalidOperationException();
            }
        }

        public IEnumerable<IMessage> UpdateMessage(IMessage message)
        {
            try
            {
                return channel.UpdateMessage(message);
            }
            catch (Exception e)
            {
                this.OnPluginError(e);
                throw new InvalidOperationException();
            }
        }

        public bool DownloadAttachment(IAttachment attachment)
        {
            try
            {
                return channel.DownloadAttachment(attachment);
            }
            catch (Exception e)
            {
                this.OnPluginError(e);
                return false;
            }
        }

        public bool SendMessage(IMessage message)
        {
            try
            {
                return channel.SendMessage(message);
            }
            catch (Exception e)
            {
                this.OnPluginError(e);
                return false;
            }
        }

        public bool ReplyMessage(IMessage original, IMessage reply)
        {
            try
            {
                return channel.ReplyMessage(original, reply);
            }
            catch (Exception e)
            {
                this.OnPluginError(e);
                return false;
            }
        }

        public void CancelBlockingCall()
        {
            try
            {
                channel.CancelBlockingCall();
            }
            catch (Exception e)
            {
                this.OnPluginError(e);
            }
        }

        public event PluginErrorEventHandler ErrorOccur;

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            try
            {
                channel.ErrorOccur -= channel_ErrorOccur;
                channel.Dispose();
            }
            catch (Exception e)
            {
                this.OnPluginError(e);
            }
        }

        #endregion
    }
}
